package com.mycompany.app.util.jbpm;

import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.db.GraphSession;
import org.jbpm.graph.def.ProcessDefinition;

import com.mycompany.app.testsuite.BaseTestUtils;
import com.mycompany.app.util.SpringUtils;

public class ProcessDefinitionDeployer extends BaseTestUtils {

    public void deploy() {

        JbpmConfiguration jt = (JbpmConfiguration) SpringUtils
                .getApplicationContext().getBean("jbpmConfiguration");
        ProcessDefinition definition = (ProcessDefinition) SpringUtils
                .getApplicationContext().getBean("leaveProcessDefinition");
        JbpmContext context = jt.createJbpmContext();
        context.deployProcessDefinition(definition);
        context.close();
    }

    public void undeploy() {

        JbpmConfiguration jt = (JbpmConfiguration) SpringUtils
                .getApplicationContext().getBean("jbpmConfiguration");
        ProcessDefinition definition = (ProcessDefinition) SpringUtils
                .getApplicationContext().getBean("leaveProcessDefinition");
        JbpmContext context = jt.createJbpmContext();
        
        ProcessDefinition processDefinition = context.getGraphSession()
                .findLatestProcessDefinition(definition.getName());
        context.getGraphSession().deleteProcessDefinition(processDefinition);
        context.close();
    }

    public static void main(String[] args) {

       ProcessDefinitionDeployer deploy=new ProcessDefinitionDeployer();
//       deploy.deploy();
       deploy.undeploy();

    }

}
